from hashlib import md5

from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse

from admins.models import TUser


# 登录
def login(request):
    if request.POST:
        userEmail = request.POST.get("userEmail")

        pwd = request.POST.get("pwd")
        password = md5(str(pwd).encode()).hexdigest()

        user = TUser.objects.filter(user_email=userEmail, user_password=password, user_status='0')

        if user.exists():
            request.session['is_login'] = True
            for us in user:
                request.session['user_name'] = us.user_name
                request.session['user_role'] = us.user_role.id

            return redirect(reverse("Users:getList"))
        else:
            msg = "用户名或密码错误"

    return render(request, "admins/login.html", locals())


# 退出
def logout(request):
    request.session.clear()
    return render(request, "admins/login.html", locals())
